package com.longer.playhelper.presenter;

import android.util.Log;


import com.blankj.utilcode.utils.RegexUtils;
import com.longer.playhelper.Application;
import com.longer.playhelper.modle.bean.User;
import com.longer.playhelper.modle.biz.UserBiz;
import com.longer.playhelper.view.activity.Register_Activity;
import com.longer.playhelper.view.iview.IRegister_ActivityView;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by longer on 2017/4/27.
 */

public class Register_ActivityPresenter {

    private IRegister_ActivityView registerActivityView;
    private User.UserBiz userBiz;

    public Register_ActivityPresenter(Register_Activity registerActivity) {
        this.registerActivityView = registerActivity;
        userBiz = new UserBiz();
    }

    public boolean IsPhone() {
        if (RegexUtils.isMobileExact(registerActivityView.getPhone())) {
            return true;
        }
        return false;
    }

    public void sendSMS() {
        registerActivityView.showSendSMSdialog();
        //先验证是否已经注册过的
        BmobQuery<User> query = new BmobQuery<User>();
        query.addWhereEqualTo("mobilePhoneNumber", registerActivityView.getPhone());
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e == null) {
                    if (list != null && list.size() != 0) {
                        if (list.get(0).getMobilePhoneNumberVerified() != null && list.get(0).getMobilePhoneNumberVerified()) {
                            //  1.表示已经注册过的手机号，返回，提示已经注册过了
                            registerActivityView.hideSendSMSdialog();
                            registerActivityView.showUserhaveRegister();
                            return;
                        }
                    } else {
                        //  3.表示没有过登记，相当于是新用户，可以直接注册
                        SendSMSNoInfor();
                    }
                } else {
                    e.printStackTrace();
                    registerActivityView.showRegisterError(e.getErrorCode());
                }
            }
        });
    }

    private void SendSMSNoInfor() {
        //发送短信
        BmobSMS.requestSMSCode(registerActivityView.getPhone(), "登录验证", new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId, BmobException ex) {
                registerActivityView.hideSendSMSdialog();
                if (ex == null) {//验证码发送成功
                    Log.i("smile", "短信id：" + smsId);//用于查询本次短信发送详情
                    registerActivityView.sendSMSSuccess();
                } else {
                    Log.d("tip", "发送短信失败：" + ex.toString());
                    ex.printStackTrace();
                    registerActivityView.sendSMSFailed(ex.getErrorCode());
                }
            }
        });
    }

    public void register() {
            registerNewUser();
    }

    public void registerNewUser() {
        User user = new User();
        user.setMobilePhoneNumber(registerActivityView.getPhone());//设置手机号码（必填）
        user.setPassword(registerActivityView.getpwd());  //设置用户密码(就设置为手机号)
        user.setPwd(registerActivityView.getpwd());
        user.signOrLogin(registerActivityView.getYzm(), new SaveListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null) {
                    Log.i("smile", "注册通过");
                    registerActivityView.RegisterSuccess();
                    login();
                } else {
                    Log.i("smile", "注册失败" + e.getMessage());
                    registerActivityView.showRegisterError(e.getErrorCode());
                }
            }

        });
    }
    private void login() {
        BmobUser.loginByAccount(registerActivityView.getPhone(), registerActivityView.getpwd(), new LogInListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (user != null) {
                    Log.i("smile", "用户登陆成功");
                    Application.setuser(user);
                    registerActivityView.registerSuccess();
                    registerActivityView.toMain_Activity();
                } else {
                    Log.i("smile", "用户自动登陆失败");
                    e.printStackTrace();
                    registerActivityView.registerFailed();
                }
            }
        });
    }
}
